package nc.hw;

import java.util.Arrays;

public class HJ30 {

    public static void main(String[] args) {
        System.out.println(mergeAndTransfer("123 15"));
//        System.out.println(transfer('7'));
    }

    private static String mergeAndTransfer(String originString) {
        String strings = originString.replace(" ", "");
        char[] odds = new char[(strings.length() + 1) / 2];
        char[] evens = new char[strings.length() / 2];
        for (int i = 0; i < strings.length(); i++) {
            if (i % 2 == 0) {
                odds[i / 2] = strings.charAt(i);
            } else {
                evens[i / 2] = strings.charAt(i);
            }
        }
        Arrays.sort(odds);
        Arrays.sort(evens);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length(); i++) {
            // 数字转换
            String transfer = transfer(i % 2 == 0 ? odds[i / 2] : evens[i / 2]);
            sb.append(transfer);
        }
        return sb.toString();
    }

    private static String transfer(char c) {
        String needTransfers = "0123456789abcdefABCDEF";
        int i = needTransfers.indexOf(c);
        if (i == -1) {
            return c+"";
        }
        // 16进制
        int[] hexValues = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 10,11,12,13,14,15};
        int hexValue = hexValues[i];
        // 转换顺序
        int newValue = 0;
        for (int j = 0; j < 4; j++) {
            newValue = newValue * 2 + hexValue % 2;
            hexValue = hexValue / 2;
        }
        return Integer.toHexString(newValue).toUpperCase();
    }

}
